<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SetController extends Controller
{   //个人资料
    public function getSet()
    {
        $id = session('id');
        $res = DB::table('users')->where('id',$id)->first();
        $did = DB::table('gonggao')->get();
        $dif = DB::table('link')->get();
        return view('geren.set',['users'=>$res,'lianjie'=>$dif,'aaa'=>$did]);
    }
    //修改个人资料
    public function getEdits()
    {
        // dd(222);
         $id = session('id');
        $res = DB::table('users')->where('id',$id)->first();
        $did = DB::table('gonggao')->get();
        $dif = DB::table('link')->get();
        //dd($res);
        return view('geren.edit',['users'=>$res,'lianjie'=>$dif,'aaa'=>$did]);
    }
    //修改
    public function postUpdates(Request $request)
    {

         //session id
        $id= $request->input('id');
        //dd($id);
        //接收form
        //接收用户
        $data=$request->except(['_token']);
        //dd($data);
        //用 session id查 原来的数据
        $res = DB::table('users')->where('id',$id)->first();
        $data['username'] = $res->username;
        $data['email'] = $res->email;
        $data['token'] = $res->token;
        //dd($data);
        //判断传过来的密码与数据库相同
        $res=DB::table('users')->where('id',$id)->update($data);
      
        if($res){
            //成功 页面跳转到列表页
            return redirect(url('/gerens/set'))->with('success','用户修改成功');
        }else{
            //失败 跳转到添加页
            return back()->with('error','用户修改失败');
        }
    } 
    //修改密码
     //修改密码
    public function getXiugai()
    {
         $did = DB::table('gonggao')->get();
        $dif = DB::table('link')->get();
        return view('geren.edits',['lianjie'=>$dif,'aaa'=>$did]);
    }  

     
    


    public function postUpdate(Request $request)
    {

        //验证
         $this->validate($request, [
                
                'newpassword' => 'required|regex:[^\w{6,18}$]',
                'repassword' => 'required|same:repassword',
                
            ],[
                'newpassword.required'=>'密码必须填写',
                'newpassword.regex'=>'密码必须为6-18位字母数字下划线',
                'repassword.required'=>'确认密码必须填写',
                'repassword.same'=>'两次密码必须一致'
            
            ]);
         //session id
        $id= session('id');
        // dd($id);
        //接收form
        $data = $request->only(['password','newpassword','token']);
        // dd($data);
        //用 session id查 原来的数据
        $res = DB::table('users')->where('id',$id)->first();
        //判断传过来的密码与数据库相同
        if(Hash::check($data['password'],$res->password)){
            //重新赋值
            $res->password = Hash::make($data['newpassword']);
            $res->token= str_random(50);
            //把对象变数组
            $new = [];
            foreach ($res as $k => $v) {
                $new[$k] = $v;
            }
            //修改
            //dd($new);
            $edit = DB::table('users')->where('id',$id)->update($new);
            if($edit){
                return redirect('/geren')->with('success','修改成功,请重新登陆');
            }else{
                return back()->with('error','修改失败');
            }
        }else{
            return back()->with('error','原始密码不正确');
        }
    }

         

   
        
}
